package com.zhihui.online.controller;



import com.alipay.api.domain.CategoryDTO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhihui.online.dto.category.CategoryQueryDTO;
import com.zhihui.online.dto.category.CategorySaveDTO;
import com.zhihui.online.dto.category.CategoryUpdateDTO;
import com.zhihui.online.entity.Category;
import com.zhihui.online.exception.MyCustomerException;
import com.zhihui.online.service.CategoryService;
import com.zhihui.online.utils.Result;
import com.zhihui.online.utils.ResultCodeEnum;
import com.zhihui.online.vo.CategoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程分类表 前端控制器
 * </p>
 *
 * @author 程序员阿良,21岁纯情后爸,懒得想名字
 * @since 2024-08-01
 */
@RestController
@Api(tags = "课程分类")
@RequestMapping("/online/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @ApiOperation("添加分类")
    @PostMapping("/addCategory")
    public Result addCategory(@RequestBody CategorySaveDTO categorySaveDTO){

        Category category = new Category();
        BeanUtils.copyProperties(categorySaveDTO,category);
        boolean b=this.categoryService.save(category);
        return b?Result.ok():Result.fail();

    }


    @ApiOperation("全部分类")
    @GetMapping("/findAll")
    public Result findAll(){
        List<Category> list=this.categoryService.list();
        return Result.ok(list);

    }
    @ApiOperation("根据id移除一个分类")
    @DeleteMapping("/removeCategory/{id}")
    public Result  removeCategory(@PathVariable Long id){
        boolean b=this.categoryService.removeById(id);
        return b?Result.ok():Result.fail();

    }
    @ApiOperation("条件和分页查询")
    @PostMapping("/{page}/{limit}")
    public Result findCategoryPage(@PathVariable Long page, @PathVariable Long limit, CategoryQueryDTO categoryQueryDTO){
        IPage<CategoryVO> page1=new Page<>(page,limit);
        page1=this.categoryService.seletPage(page1, categoryQueryDTO);
        return Result.ok(page1);
    }
    @ApiOperation("根据id得到分类")
    @GetMapping("/findCategoryById/{id}")
    public Result findCategoryById(@PathVariable Long id){

        Category category = this.categoryService.getById(id);

        return Result.ok(category);
    }
    @ApiOperation("批量删除")
    @DeleteMapping("/removeCategoryByIds")
    public Result removeCategoryByIds(@RequestBody List<Long> ids){
    boolean b=this.categoryService.removeByIds(ids);
    return b?Result.ok():Result.fail();

    }
    @ApiOperation("修改")
    @PostMapping("/updateCategory")
    public Result updateCategory(@RequestBody CategoryUpdateDTO categoryUpdateDTO){
        if (categoryUpdateDTO == null || categoryUpdateDTO.getId() == null) {
            throw new MyCustomerException(ResultCodeEnum.DATA_ERROR);
        }
        Category category=new Category();
        BeanUtils.copyProperties(categoryUpdateDTO, category);
    boolean b=this.categoryService.updateById(category);
    return b?Result.ok():Result.fail();

    }
}

